/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_te_analyze.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include <tongeasy/config.h>
#include "../test_tongeasy_comm.h"
#include <tongeasy/te_core.h>
#include <tongeasy/te_comm.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_te_create_ct(&p_st_conn, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(te_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    Frvar_field *p_st_field = NULL;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    p_list_report = &((&st_hash_report)->p_list_array[0]);

    p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_node_report);

    p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
    CU_ASSERT_PTR_NOT_NULL(p_st_report_data);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "call call_req_fun1");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_REQ_SERVICE_NAME, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "call_req_fun1");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_REQ_SERVICE_METHOD, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "call");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_REQ_PACKET_TYPE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "5Z0200");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_REQ_ORIGINAL_NODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "democli");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_REQ_SEND_NODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "democli");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_REQ_RECV_NODE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
    CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "demosvr");

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_TID, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 73L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_TE_REPORT_SUBTID, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_DTS_REPORT_REQ_ARG_SIZE, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_START_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_END_TIMESTAMP, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 1454549354123L);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_SEND_TIME_US, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 0);

    p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_REQ_DATA_BYTES, 0);
    CU_ASSERT_EQUAL(p_st_field->em_data_type, FRLONG);
    CU_ASSERT_EQUAL(p_st_field->field_value.ll_value, 206L);

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_te_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_te_analyze(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" te_analyze()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
